﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using OpenCvSharp;
using Window = System.Windows.Window;

namespace OpenCV_Start
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    { 
        public BitmapSource Picture { set; get; }

  
        public MainWindow()
        {
            InitializeComponent();        
            Mat m = Cv2.ImRead(@".\resource\lena.jpg");
            Picture = OpenCvSharp.WpfExtensions.BitmapSourceConverter.ToBitmapSource(m);
            this.DataContext = this;
        }

       
        private void RunCamera_Click(object sender, RoutedEventArgs e)
        {
            VideoCapture vc = new VideoCapture(0);
            if (!vc.IsOpened())
            {
                MessageBox.Show("没有摄像头,请检查");
                return;
            }
           
                Mat video_picture=new Mat();
                Task.Run(() =>
                {
                    while (vc.Read(video_picture))
                    {
                        this.Dispatcher.Invoke(() =>
                        {
                            videorun.Source = OpenCvSharp.WpfExtensions.BitmapSourceConverter.ToBitmapSource(video_picture);

                        });
                    };
                });
        

        }
    }
}
   

